/*
 * CmdClient.java
 *
 * Created on March 4, 2007, 6:28 PM
 */

package astrolabe;

import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import org.jfarrell.classes.Command;

/**
 *
 * @author Tobias Knutsson
 */
public class CmdClient {
    
    Socket sock;
    ObjectOutputStream out;
    Image awtImg2 = Toolkit.getDefaultToolkit().getImage("easy_small.jpg");
    Point endPos = new Point(200,150);
        
    //private Command path = new Command('p', awtImg, endPos);
    private Command path2 = new Command('p', awtImg2, endPos);
    
    private Command stop = new Command('s');
    private Command quit = new Command('q');
    
    public CmdClient() {
        
        try {
               sock = new Socket("localhost", 2222);
               System.out.println("CLIENT: Connection accepted.");
               out = new ObjectOutputStream(sock.getOutputStream());
               System.out.println("CLIENT: Created ObjectOutputStream.");
        }
        catch (IOException e) {
            System.out.println(e);
        }
        ui();
    }
    
    public void ui()
    {
        Scanner kb = new Scanner(System.in);
        String cmd;
        
        while(true)
        {
                System.out.println("CLIENT: cmd>");
                    cmd=kb.nextLine();
                System.out.println();
                sendCmd(cmd);

        }
    }
    
    public void sendCmd(String cmd)
    {
        try
        {
            System.out.println("Sending command: "+cmd);
            if(cmd.charAt(0)=='p')
            {
                Image awtImg = Toolkit.getDefaultToolkit().getImage("camImage0"+Integer.parseInt(cmd.substring(1))+".jpg");
                out.writeObject(new Command('p',awtImg, endPos));
            }
                
            //else if(cmd.charAt(0)=='q')
            //    out.writeObject(path2);
            else
                out.writeObject(new Command(cmd.charAt(0), Integer.parseInt(cmd.substring(1))));
                //out.writeObject(new Command(cmd));
        }catch(IOException e)
        {
            System.out.println("Could not send command: "+cmd);
        }
    }
    
    public static void main(String args[])
    {
        CmdClient cmdc = new CmdClient();
        
        
    }
}
